home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / perl5 / Debconf / ConfModule.pm < prev    next >
Encoding:
Perl POD Document  |  2007-03-24  |  14.6 KB  |  632 lines

  1. #!/usr/bin/perl -w
  2. # This file was preprocessed, do not edit!
  3.  
  4.  
  5. package Debconf::ConfModule;
  6. use strict;
  7. use IPC::Open2;
  8. use FileHandle;
  9. use Debconf::Gettext;
  10. use Debconf::Config;
  11. use Debconf::Question;
  12. use Debconf::Priority qw(priority_valid high_enough);
  13. use Debconf::FrontEnd::Noninteractive;
  14. use Debconf::Log ':all';
  15. use Debconf::Encoding;
  16. use base qw(Debconf::Base);
  17.  
  18.  
  19. my %codes = (
  20.     success => 0,
  21.     escaped_data => 1,
  22.     badparams => 10,
  23.     syntaxerror => 20,
  24.     input_invisible => 30,
  25.     version_bad => 30,
  26.     go_back => 30,
  27.     internalerror => 100,
  28. );
  29.  
  30.  
  31. sub init {
  32.     my $this=shift;
  33.  
  34.     $this->version("2.0");
  35.     
  36.     $this->owner('unknown') if ! defined $this->owner;
  37.     
  38.     $this->frontend->capb_backup('');
  39.  
  40.     $this->seen([]);
  41.     $this->busy([]);
  42.  
  43.     $ENV{DEBIAN_HAS_FRONTEND}=1;
  44. }
  45.  
  46.  
  47. sub startup {
  48.     my $this=shift;
  49.     my $confmodule=shift;
  50.  
  51.     $this->frontend->clear;
  52.     $this->busy([]);
  53.     
  54.     my @args=$this->confmodule($confmodule);
  55.     push @args, @_ if @_;
  56.     
  57.     debug developer => "starting ".join(' ',@args);
  58.     $this->pid(open2($this->read_handle(FileHandle->new),
  59.                  $this->write_handle(FileHandle->new),
  60.              @args)) || die $!;
  61.         
  62.     $this->caught_sigpipe('');
  63.     $SIG{PIPE}=sub { $this->caught_sigpipe(128) };
  64. }
  65.  
  66.  
  67. sub communicate {
  68.     my $this=shift;
  69.  
  70.     my $r=$this->read_handle;
  71.     $_=<$r> || return $this->finish;
  72.     chomp;
  73.     my $ret=$this->process_command($_);
  74.     my $w=$this->write_handle;
  75.     print $w $ret."\n";
  76.     return '' unless length $ret;
  77.     return 1;
  78. }
  79.  
  80.  
  81. sub escape {
  82.     my $text=shift;
  83.     $text=~s/\\/\\\\/g;
  84.     $text=~s/\n/\\n/g;
  85.     return $text;
  86. }
  87.  
  88.  
  89. sub unescape_split {
  90.     my $text=shift;
  91.     my @words;
  92.     my $word='';
  93.     for my $chunk (split /(\\.|\s+)/, $text) {
  94.         if ($chunk eq '\n') {
  95.             $word.="\n";
  96.         } elsif ($chunk=~/^\\(.)$/) {
  97.             $word.=$1;
  98.         } elsif ($chunk=~/^\s+$/) {
  99.             push @words, $word;
  100.             $word='';
  101.         } else {
  102.             $word.=$chunk;
  103.         }
  104.     }
  105.     push @words, $word if $word ne '';
  106.     return @words;
  107. }
  108.  
  109.  
  110. sub process_command {
  111.     my $this=shift;
  112.     
  113.     debug developer => "<-- $_";
  114.     return 1 unless defined && ! /^\s*#/; # Skip blank lines, comments.
  115.     chomp;
  116.     my ($command, @params);
  117.     if (defined $this->client_capb and grep { $_ eq 'escape' } @{$this->client_capb}) {
  118.         ($command, @params)=unescape_split($_);
  119.     } else {
  120.         ($command, @params)=split(' ', $_);
  121.     }
  122.     $command=lc($command);
  123.     if (lc($command) eq "stop") {
  124.         return $this->finish;
  125.     }
  126.     if (! $this->can("command_$command")) {
  127.         return $codes{syntaxerror}.' '.
  128.                "Unsupported command \"$command\" (full line was \"$_\") received from confmodule.";
  129.     }
  130.     $command="command_$command";
  131.     my $ret=join(' ', $this->$command(@params));
  132.     debug developer => "--> $ret";
  133.     if ($ret=~/\n/) {
  134.         debug developer => 'Warning: return value is multiline, and would break the debconf protocol. Truncating to first line.';
  135.         $ret=~s/\n.*//s;
  136.         debug developer => "--> $ret";
  137.     }
  138.     return $ret;
  139. }
  140.  
  141.  
  142. sub finish {
  143.     my $this=shift;
  144.  
  145.     waitpid $this->pid, 0 if defined $this->pid;
  146.     $this->exitcode($this->caught_sigpipe || ($? >> 8));
  147.  
  148.     $SIG{PIPE} = sub {};
  149.     
  150.     foreach (@{$this->seen}) {
  151.         my $q=Debconf::Question->get($_->name);
  152.         $_->flag('seen', 'true') if $q;
  153.     }
  154.     $this->seen([]);
  155.     
  156.     return '';
  157. }
  158.  
  159.  
  160. sub command_input {
  161.     my $this=shift;
  162.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
  163.     my $priority=shift;
  164.     my $question_name=shift;
  165.     
  166.     my $question=Debconf::Question->get($question_name) ||
  167.         return $codes{badparams}, "\"$question_name\" doesn't exist";
  168.  
  169.     if (! priority_valid($priority)) {
  170.         return $codes{syntaxerror}, "\"$priority\" is not a valid priority";
  171.     }
  172.  
  173.     $question->priority($priority);
  174.     
  175.     my $visible=1;
  176.  
  177.     if ($question->type ne 'error') {
  178.         $visible='' unless high_enough($priority);
  179.  
  180.         $visible='' if ! Debconf::Config->reshow &&
  181.                    $question->flag('seen') eq 'true';
  182.     }
  183.  
  184.     my $markseen=$visible;
  185.  
  186.     if ($visible && ! $this->frontend->interactive) {
  187.         $visible='';
  188.         $markseen='' unless Debconf::Config->noninteractive_seen eq 'true';
  189.     }
  190.  
  191.     my $element;
  192.     if ($visible) {
  193.         $element=$this->frontend->makeelement($question);
  194.         unless ($element) {
  195.             return $codes{internalerror},
  196.                    "unable to make an input element";
  197.         }
  198.  
  199.         $visible=$element->visible;
  200.     }
  201.  
  202.     if (! $visible) {
  203.         $element=Debconf::FrontEnd::Noninteractive->makeelement($question, 1);
  204.  
  205.         return $codes{input_invisible}, "question skipped" unless $element;
  206.     }
  207.  
  208.     $element->markseen($markseen);
  209.  
  210.     push @{$this->busy}, $question_name;
  211.     
  212.     $this->frontend->add($element);
  213.     if ($element->visible) {
  214.         return $codes{success}, "question will be asked";
  215.     }
  216.     else {
  217.         return $codes{input_invisible}, "question skipped";
  218.     }
  219. }
  220.  
  221.  
  222. sub command_clear {
  223.     my $this=shift;
  224.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 0;
  225.  
  226.     $this->frontend->clear;
  227.     $this->busy([]);
  228.     return $codes{success};
  229. }
  230.  
  231.  
  232. sub command_version {
  233.     my $this=shift;
  234.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ > 1;
  235.     my $version=shift;
  236.     if (defined $version) {
  237.         return $codes{version_bad}, "Version too low ($version)"
  238.             if int($version) < int($this->version);
  239.         return $codes{version_bad}, "Version too high ($version)"
  240.             if int($version) > int($this->version);
  241.     }
  242.     return $codes{success}, $this->version;
  243. }
  244.  
  245.  
  246. sub command_capb {
  247.     my $this=shift;
  248.     $this->client_capb([@_]);
  249.     $this->frontend->capb_backup(1) if grep { $_ eq 'backup' } @_;
  250.     my @capb=('multiselect', 'escape');
  251.     push @capb, $this->frontend->capb;
  252.     return $codes{success}, @capb;
  253. }
  254.  
  255.  
  256. sub command_title {
  257.     my $this=shift;
  258.     $this->frontend->title(join ' ', @_);
  259.     $this->frontend->requested_title($this->frontend->title);
  260.  
  261.     return $codes{success};
  262. }
  263.  
  264.  
  265. sub command_settitle {
  266.     my $this=shift;
  267.     
  268.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
  269.     my $question_name=shift;
  270.     
  271.     my $question=Debconf::Question->get($question_name) ||
  272.         return $codes{badparams}, "\"$question_name\" doesn't exist";
  273.  
  274.     if ($this->frontend->can('settitle')) {
  275.         $this->frontend->settitle($question);
  276.     } else {
  277.         $this->frontend->title($question->description);
  278.     }
  279.     $this->frontend->requested_title($this->frontend->title);
  280.     
  281.     return $codes{success};
  282. }
  283.  
  284.  
  285. sub command_beginblock {
  286.     return $codes{success};
  287. }
  288. sub command_endblock {
  289.     return $codes{success};
  290. }
  291.  
  292.  
  293. sub command_go {
  294.     my $this=shift;
  295.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ > 0;
  296.  
  297.     my $ret=$this->frontend->go;
  298.     if ($ret && (! $this->backed_up ||
  299.                  grep { $_->visible } @{$this->frontend->elements})) {
  300.         foreach (@{$this->frontend->elements}) {
  301.             $_->question->value($_->value);
  302.             push @{$this->seen}, $_->question if $_->markseen && $_->question;
  303.         }
  304.         $this->frontend->clear;
  305.         $this->busy([]);
  306.         $this->backed_up('');
  307.         return $codes{success}, "ok"
  308.     }
  309.     else {
  310.         $this->frontend->clear;
  311.         $this->busy([]);
  312.         $this->backed_up(1);
  313.         return $codes{go_back}, "backup";
  314.     }
  315. }
  316.  
  317.  
  318. sub command_get {
  319.     my $this=shift;
  320.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
  321.     my $question_name=shift;
  322.     my $question=Debconf::Question->get($question_name) ||
  323.         return $codes{badparams}, "$question_name doesn't exist";
  324.  
  325.     if (defined $question->value) {
  326.         if (defined $this->client_capb and grep { $_ eq 'escape' } @{$this->client_capb}) {
  327.             return $codes{escaped_data}, escape($question->value);
  328.         } else {
  329.             return $codes{success}, $question->value;
  330.         }
  331.     }
  332.     else {
  333.         return $codes{success}, '';
  334.     }
  335. }
  336.  
  337.  
  338. sub command_set {
  339.     my $this=shift;
  340.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 1;
  341.     my $question_name=shift;
  342.     my $value=join(" ", @_);
  343.  
  344.     my $question=Debconf::Question->get($question_name) ||
  345.         return $codes{badparams}, "$question_name doesn't exist";
  346.     $question->value($value);
  347.     return $codes{success}, "value set";
  348. }
  349.  
  350.  
  351. sub command_reset {
  352.     my $this=shift;
  353.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
  354.     my $question_name=shift;
  355.  
  356.     my $question=Debconf::Question->get($question_name) ||
  357.         return $codes{badparams}, "$question_name doesn't exist";
  358.     $question->value($question->default);
  359.     $question->flag('seen', 'false');
  360.     return $codes{success};
  361. }
  362.  
  363.  
  364. sub command_subst {
  365.     my $this = shift;
  366.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 2;
  367.     my $question_name = shift;
  368.     my $variable = shift;
  369.     my $value = (join ' ', @_);
  370.     
  371.     my $question=Debconf::Question->get($question_name) ||
  372.         return $codes{badparams}, "$question_name doesn't exist";
  373.     my $result=$question->variable($variable,$value);
  374.     return $codes{internalerror}, "Substitution failed" unless defined $result;
  375.     return $codes{success};
  376. }
  377.  
  378.  
  379. sub command_register {
  380.     my $this=shift;
  381.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
  382.     my $template=shift;
  383.     my $name=shift;
  384.     
  385.     my $tempobj = Debconf::Question->get($template);
  386.     if (! $tempobj) {
  387.         return $codes{badparams}, "No such template, \"$template\"";
  388.     }
  389.     my $question=Debconf::Question->get($name) || 
  390.                  Debconf::Question->new($name, $this->owner, $tempobj->type);
  391.     if (! $question) {
  392.         return $codes{internalerror}, "Internal error making question";
  393.     }
  394.     if (! defined $question->addowner($this->owner, $tempobj->type)) {
  395.         return $codes{internalerror}, "Internal error adding owner";
  396.     }
  397.     if (! $question->template($template)) {
  398.         return $codes{internalerror}, "Internal error setting template";
  399.     }
  400.  
  401.     return $codes{success};
  402. }
  403.  
  404.  
  405. sub command_unregister {
  406.     my $this=shift;
  407.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
  408.     my $name=shift;
  409.     
  410.     my $question=Debconf::Question->get($name) ||
  411.         return $codes{badparams}, "$name doesn't exist";
  412.     if (grep { $_ eq $name } @{$this->busy}) {
  413.         return $codes{badparams}, "$name is busy, cannot unregister right now";
  414.     }
  415.     $question->removeowner($this->owner);
  416.     return $codes{success};
  417. }
  418.  
  419.  
  420. sub command_purge {
  421.     my $this=shift;
  422.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ > 0;
  423.     
  424.     my $iterator=Debconf::Question->iterator;
  425.     while (my $q=$iterator->iterate) {
  426.         $q->removeowner($this->owner);
  427.     }
  428.  
  429.     return $codes{success};
  430. }
  431.  
  432.  
  433. sub command_metaget {
  434.     my $this=shift;
  435.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
  436.     my $question_name=shift;
  437.     my $field=shift;
  438.     
  439.     my $question=Debconf::Question->get($question_name) ||
  440.         return $codes{badparams}, "$question_name doesn't exist";
  441.     my $lcfield=lc $field;
  442.     my $fieldval=$question->$lcfield();
  443.     unless (defined $fieldval) {
  444.         return $codes{badparams}, "$field does not exist";
  445.     }
  446.     if (defined $this->client_capb and grep { $_ eq 'escape' } @{$this->client_capb}) {
  447.         return $codes{escaped_data}, escape($fieldval);
  448.     } else {
  449.         return $codes{success}, $fieldval;
  450.     }
  451. }
  452.  
  453.  
  454. sub command_fget {
  455.     my $this=shift;
  456.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
  457.     my $question_name=shift;
  458.     my $flag=shift;
  459.     
  460.     my $question=Debconf::Question->get($question_name) ||
  461.         return $codes{badparams},  "$question_name doesn't exist";
  462.         
  463.     return $codes{success}, $question->flag($flag);
  464. }
  465.  
  466.  
  467. sub command_fset {
  468.     my $this=shift;
  469.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 3;
  470.     my $question_name=shift;
  471.     my $flag=shift;
  472.     my $value=(join ' ', @_);
  473.     
  474.     my $question=Debconf::Question->get($question_name) ||
  475.         return $codes{badparams}, "$question_name doesn't exist";
  476.  
  477.     if ($flag eq 'seen') {
  478.         $this->seen([grep {$_ ne $question} @{$this->seen}]);
  479.     }
  480.         
  481.     return $codes{success}, $question->flag($flag, $value);
  482. }
  483.  
  484.  
  485. sub command_info {
  486.     my $this=shift;
  487.  
  488.     if (@_ == 0) {
  489.         $this->frontend->info(undef);
  490.     } elsif (@_ == 1) {
  491.         my $question_name=shift;
  492.  
  493.         my $question=Debconf::Question->get($question_name) ||
  494.             return $codes{badparams}, "\"$question_name\" doesn't exist";
  495.  
  496.         $this->frontend->info($question);
  497.     } else {
  498.         return $codes{syntaxerror}, "Incorrect number of arguments";
  499.     }
  500.  
  501.     return $codes{success};
  502. }
  503.  
  504.  
  505. sub command_progress {
  506.     my $this=shift;
  507.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 1;
  508.     my $subcommand=shift;
  509.     $subcommand=lc($subcommand);
  510.     
  511.     if ($subcommand eq 'start') {
  512.         return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 3;
  513.         my $min=shift;
  514.         my $max=shift;
  515.         my $question_name=shift;
  516.  
  517.         return $codes{syntaxerror}, "min ($min) > max ($max)" if $min > $max;
  518.  
  519.         my $question=Debconf::Question->get($question_name) ||
  520.             return $codes{badparams}, "$question_name doesn't exist";
  521.  
  522.         $this->frontend->progress_start($min, $max, $question);
  523.     }
  524.     elsif ($subcommand eq 'set') {
  525.         return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
  526.         my $value=shift;
  527.         $this->frontend->progress_set($value);
  528.     }
  529.     elsif ($subcommand eq 'step') {
  530.         return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
  531.         my $inc=shift;
  532.         $this->frontend->progress_step($inc);
  533.     }
  534.     elsif ($subcommand eq 'info') {
  535.         return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
  536.         my $question_name=shift;
  537.  
  538.         my $question=Debconf::Question->get($question_name) ||
  539.             return $codes{badparams}, "$question_name doesn't exist";
  540.  
  541.         $this->frontend->progress_info($question);
  542.     }
  543.     elsif ($subcommand eq 'stop') {
  544.         return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 0;
  545.         $this->frontend->progress_stop();
  546.     }
  547.     else {
  548.         return $codes{syntaxerror}, "Unknown subcommand";
  549.     }
  550.  
  551.     return $codes{success}, "OK";
  552. }
  553.  
  554.  
  555. sub command_data {
  556.     my $this=shift;
  557.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 3;
  558.     my $template=shift;
  559.     my $item=shift;
  560.     my $value=join(' ', @_);
  561.     $value=~s/\\([n"\\])/($1 eq 'n') ? "\n" : $1/eg;
  562.  
  563.     my $tempobj=Debconf::Template->get($template);
  564.     if (! $tempobj) {
  565.         if ($item ne 'type') {
  566.             return $codes{badparams}, "Template data field '$item' received before type field";
  567.         }
  568.         $tempobj=Debconf::Template->new($template, $this->owner, $value);
  569.         if (! $tempobj) {
  570.             return $codes{internalerror}, "Internal error making template";
  571.         }
  572.     } else {
  573.         if ($item eq 'type') {
  574.             return $codes{badparams}, "Template type already set";
  575.         }
  576.         $tempobj->$item(Debconf::Encoding::convert("UTF-8", $value));
  577.     }
  578.  
  579.     return $codes{success};
  580. }
  581.  
  582.  
  583. sub command_visible {
  584.     my $this=shift;
  585.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
  586.     my $priority=shift;
  587.     my $question_name=shift;
  588.     
  589.     my $question=Debconf::Question->get($question_name) ||
  590.         return $codes{badparams}, "$question_name doesn't exist";
  591.     return $codes{success}, $this->frontend->visible($question, $priority) ? "true" : "false";
  592. }
  593.  
  594.  
  595. sub command_exist {
  596.     my $this=shift;
  597.     return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
  598.     my $question_name=shift;
  599.     
  600.     return $codes{success}, 
  601.         Debconf::Question->get($question_name) ? "true" : "false";
  602. }
  603.  
  604.  
  605. sub AUTOLOAD {
  606.     (my $field = our $AUTOLOAD) =~ s/.*://;
  607.  
  608.     no strict 'refs';
  609.     *$AUTOLOAD = sub {
  610.         my $this=shift;
  611.         
  612.         return $this->{$field} unless @_;
  613.         return $this->{$field}=shift;
  614.     };
  615.     goto &$AUTOLOAD;
  616. }
  617.  
  618.  
  619. sub DESTROY {
  620.     my $this=shift;
  621.     
  622.     $this->read_handle->close if $this->read_handle;
  623.     $this->write_handle->close if $this->write_handle;
  624.     
  625.     if (defined $this->pid && $this->pid > 1) {
  626.         kill 'TERM', $this->pid;
  627.     }
  628. }
  629.  
  630.  
  631. 1
  632.